package com.example.administrator.ttphonehelper.camera;

import android.content.res.Configuration;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;

import com.example.administrator.ttphonehelper.R;

public class CameraActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{
    private SeekBar mSBZoom;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mHolder;

    private android.hardware.Camera mCamera;
    private android.hardware.Camera.Parameters mParameters;
    private int mMaxZoom;
    private int mFlashState;//闪光灯状态   0 自动  1 开  2 关     存储用户的偏好设置
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        initSurfaceView();
    }

    private void initSurfaceView() {
        mSBZoom = (SeekBar) findViewById(R.id.seekBar);
        //拿到控制器
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(new SurfaceHolder.Callback() {
            /*
             * surface创建的时候
             *  * a.打开摄像头
                * b.拿到摄像头的Params
                * c.将摄像头的取象投影到surfaceView
             */
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                 //打开摄像头
                mCamera = Camera.open();
                //得到摄像头参数
                mParameters = mCamera.getParameters();
                /*
                 * 焦距
                 */
                //最大焦距
                mMaxZoom = mParameters.getMaxZoom();
                mSBZoom.setMax(mMaxZoom);
                //闪光灯
                if(mParameters.getFlashMode()==null){
                    Toast.makeText(CameraActivity.this,"没有闪光灯",Toast.LENGTH_SHORT).show();
                }
                else{

                }

                //判断横竖屏  在电脑上面需要设置，让投影旋转90度
                if(getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE)
                {
                    mParameters.set("orientation", "portrait");
                    mCamera.setDisplayOrientation(90);
                }
                else
                {
                    mParameters.set("orientation", "landscape");
                    mCamera.setDisplayOrientation(0);
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }

            });
    }

    //点击事件（布局里面声明）
    public void doClick(View v){
        switch (v.getId()){
            case R.id.photograph:

                break;
            case R.id.flash:

                break;
            case R.id.back:
                finish();
                break;

        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}
